// pages/indexMyZtPosition/indexMyZtPosition.js
let url_js = require("../../utils/url.js");
let common_js = require("../../utils/common.js");
let const_js = require("../../utils/const.js");
let user_js = require("../../utils/user.js");
let ubt_js = require("../../utils/ubt.js");
let name_js = require("../../utils/ubtButtonName.js");
let api = require("../../utils/apiRequest.js");
let notificationCenter = require('../../thridLib/WxNotificationCenter.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    locationObj: null,
    userInfo: null,
    contentStatus: -1, //1有数据
    ztList: [],
    pageInfo: {
      curPage: 1,
      pageSize: 10,
      noMore: false
    },
    myZtdIndex: -1,
    changedZtd: null //点击切换自提点
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //业务埋点
    ubt_js.myPageView(common_js.currentPageName(), options);
    //1.禁止转发
    wx.hideShareMenu();
    //2.初始化数据
    this.initData(options);
    //3.获取数据
    this.requestToGetMyShops(this.data.pageInfo.curPage, this.data.pageInfo.pageSize);
  },

  //初始化数据
  initData: function(options) {
    let tempUserInfo = user_js.userInfo();
    let tmpLocationObj = common_js.jsonParse(options.locationInfo);
    this.setData({
      locationObj: tmpLocationObj,
      userInfo: tempUserInfo
    });
  },

  //网络请求获取数据
  requestToGetMyShops: function(curPage, pageSize, pullDownRefresh) {
    //1.我的自提点
    let myShopsData = {
      code: user_js.getCode(),
      curPage: curPage,
      pageSize: pageSize,
      geoLat: this.data.locationObj.lat,
      geoLon: this.data.locationObj.lon
    }
    api.wxRequest(url_js.shopMyShopsUrl, myShopsData).then(res => {
      this.handleMyShopsData(res, curPage, pullDownRefresh);
      common_js.hidePageLoading(pullDownRefresh);
    }).catch(err => {
      common_js.hidePageLoading(pullDownRefresh);
      common_js.log(err);
    });
  },

  //处理返回的我的自提点列表
  handleMyShopsData: function(res, curPage, pullDownRefresh) {
    //设置数据
    let tmpList = this.data.ztList;
    if (pullDownRefresh) {
      tmpList = [];
    }
    //pageInfo
    let tmpPageInfo = this.data.pageInfo;
    if (res.page.pages <= curPage) {
      tmpPageInfo.noMore = true;
    } else {
      tmpPageInfo.curPage++;
    }
    //list
    if (res.result.length > 0) {
      tmpList = tmpList.concat(res.result);
      //获取当前自提点的index
      let tmpIndex = this.data.myZtdIndex;
      if (this.data.myZtdIndex == -1) {
        tmpIndex = this.getMyZtdIndex(tmpList);
      }
      this.setData({
        contentStatus: 1,
        myZtdIndex: tmpIndex,
        pageInfo: tmpPageInfo,
        ztList: tmpList
      });
    } else {
      this.setData({
        contentStatus: 1,
        myZtdIndex: tmpIndex,
        pageInfo: tmpPageInfo
      });
    }
  },

  //获取当前自提点的index
  getMyZtdIndex: function(list) {
    let ztdIndex = -1;
    if (list.length > 0) {
      list.forEach((item, index) => {
        if (item.selected) {
          ztdIndex = index;
        }
      });
    }
    return ztdIndex;
  },

  //删除我的自提点shopDeleteShopUrl
  requestToDeleteShop: function(cityId, shopId, index) {
    //1.我的自提点
    let data = {
      cityId: cityId,
      code: user_js.getCode(),
      shopId: shopId
    }
    api.wxRequest(url_js.shopDeleteShopUrl, data).then(res => {
      this.handleDeleteShopData(res, index);
    }).catch(err => {
      common_js.log(err);
    });
  },

  //处理返回的我的自提点列表
  handleDeleteShopData: function(res, index) {
    //设置数据
    let tmpList = this.data.ztList;
    tmpList.splice(index, 1);
    //list
    this.setData({
      ztList: tmpList
    });
  },

  //选择自提点，将刚才的自提点信息返回
  requestToSelectShop: function(shopId, index) {
    let tmpSelectItem = this.data.selectedItem;
    let data = {
      code: this.data.userInfo.code,
      shopId: shopId,
      geoLat: this.data.locationObj.lat,
      geoLon: this.data.locationObj.lon
    }
    api.wxRequest(url_js.shopSelectShopUrl, data).then(res => {
      this.handleSelectShopData(res, index);
    }).catch(err => {
      common_js.log(err);
    });
  },

  //处理选中自提点返回的数据
  handleSelectShopData: function(res, index) {
    if (this.data.myZtdIndex != -1) {
      let lastItem = this.data.ztList[this.data.myZtdIndex];
      lastItem.selected = !lastItem.selected;
    }
    let item = this.data.ztList[index];
    item.selected = true;

    //1.存储新的自提点
    let tmpUserInfo = this.data.userInfo;
    tmpUserInfo.ztd = item;
    user_js.saveUserInfo(tmpUserInfo);
    //set data
    this.setData({
      myZtdIndex: index,
      changedZtd: item,
      userInfo: tmpUserInfo,
      ztList: this.data.ztList
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    //1.如果已经切换了自提点，发送通知
    if (this.data.changedZtd) {
      //1.. post notification
      notificationCenter.postNotificationName(const_js.changeShopSucessNoti);
      //2.跟新上个页面的我的自提点信息
      let pages = getCurrentPages();
      let prePage = pages[pages.length - 2];
      prePage.updateMyZtdInfo(this.data.changedZtd);
    }
    // 业务埋点
    ubt_js.myPageViewOut(common_js.currentPageName());
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    //check 
    if (this.data.contentStatus != 1) {
      wx.stopPullDownRefresh();
      return
    }
    //2.reset data
    let tmpPageInfo = this.data.pageInfo;
    tmpPageInfo.curPage = 1;
    tmpPageInfo.noMore = false;
    this.setData({
      pageInfo: tmpPageInfo
    });
    //3.request
    this.requestToGetMyShops(this.data.pageInfo.curPage, this.data.pageInfo.pageSize, true);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.pageInfo.noMore) return;
    this.requestToGetMyShops(this.data.pageInfo.curPage, this.data.pageInfo.pageSize);
  },


  //联系团长
  onManager: function(e) {
    let index = e.currentTarget.dataset.index;
    let item = this.data.ztList[index];
    if (!item.mobile) {
      common_js.showToast("电话不存在！");
      return
    }
    //打电话
    wx.makePhoneCall({
      phoneNumber: item.mobile
    })
  },

  //删除
  onDelete: function(e) {
    let index = e.currentTarget.dataset.index;
    let item = this.data.ztList[index];
    this.requestToDeleteShop(item.cityId, item.id, index);
  },

  //切换
  onChange: function(e) {
    //埋点
    ubt_js.myClickButton(common_js.currentPageName(), name_js.indexMyZtPositionChange);
    
    let index = e.currentTarget.dataset.index;
    let item = this.data.ztList[index];
    this.requestToSelectShop(item.id, index);
  }
})